<template>
	<view class="pagesContent">
		<view class="pagesContentView">
			<view class="pageList">
				<view class="items">
					<view class="left">相册读取权限</view>
					<view class="right">
						<view v-if="READ_EXTERNAL_STORAGE  == 0"
							@click="selectitem('android.permission.READ_EXTERNAL_STORAGE')">去设置</view>
						<view v-if="READ_EXTERNAL_STORAGE  == 1"
							@click="selectitem('android.permission.READ_EXTERNAL_STORAGE')">已获得授权</view>
						<view v-if="READ_EXTERNAL_STORAGE  == -1" @click="openPermission(1)">被永久拒绝权限</view>
						<image src="@/static/images/right_2.png" mode=""></image>
					</view>
				</view>

				<view class="items">
					<view class="left">照片储存权限</view>
					<view class="right">
						<view v-if="WRITE_EXTERNAL_STORAGE  == 0"
							@click="selectitem('android.permission.WRITE_EXTERNAL_STORAGE')">去设置</view>
						<view v-if="WRITE_EXTERNAL_STORAGE  == 1"
							@click="selectitem('android.permission.WRITE_EXTERNAL_STORAGE')"> 已获得授权</view>
						<view v-if="WRITE_EXTERNAL_STORAGE  == -1" @click="openPermission(2)">被永久拒绝权限</view>
						<image src="@/static/images/right_2.png" mode=""></image>
					</view>
				</view>
				<view class="items">
					<view class="left">关闭广告</view>
					<view class="right">
						<switch checked color="#0145BB" @change="switch2Change" :checked="ad_switch"
							style="transform:scale(0.7)" />
					</view>
				</view>
				<view class="items">
					<view class="left">关闭定向推送</view>
					<view class="right">
						<switch checked color="#0145BB" @change="switch3Change" :checked="pushSwitch"
							style="transform:scale(0.7)" />
					</view>
				</view>
			</view>

			<view class="adBox">
				<adView></adView>
			</view>
		</view>

	</view>
</template>
<script>
	import {
		mapGetters,
		mapMutations,
		mapActions
	} from 'vuex';
	// #ifdef APP
	import permision from "@/js_sdk/wa-permission/permission.js"
	// #endif

	export default {
		data() {
			return {
				title: "系统权限",
				CAMERA: 0,
				READ_EXTERNAL_STORAGE: 0,
				WRITE_EXTERNAL_STORAGE: 0,
				ACCESS_FINE_LOCATION: 0,
				pushSwitch: false

			}
		},
		onLoad() {
			this.judgeIosPermission("android.permission.READ_EXTERNAL_STORAGE")
			this.judgeIosPermission("android.permission.WRITE_EXTERNAL_STORAGE")
		},
		onShow() {
			this.$nextTick(() => {
				// //动向推送开关
				let pushSwitch = uni.getStorageSync("pushSwitch")
				console.log(pushSwitch)

				if (pushSwitch) {
					this.pushSwitch = pushSwitch
				} else {
					this.pushSwitch = false
				}
			})

		},
		computed: {
			...mapGetters(["appVipSwitch", "ad_switch", "adpid"])
		},
		methods: {
			...mapMutations(["updataAdSwitch", "updataPushSwitch"]),
			switch2Change(e) {				
				this.checked = e.detail.value
				uni.setStorageSync("userSetAdSwitch", JSON.stringify(this.checked))
				this.updataAdSwitch(this.checked)
			},

			switch3Change(e) {				
				this.pushSwitch = e.detail.value
				uni.setStorageSync("pushSwitch", this.pushSwitch)
			},
			openPermission(index) {
				let message = ""
				switch (index) {
					case 0:
						message = ""
						break;
					case 1:
						message = "媒体和文件权限"
						break;
					case 2:
						message = "媒体和文件权限"
						break;
					case 3:
						message = "位置信息权限"
						break;
				}
				console.log(index, message)


				// 打开权限设置界面
				uni.showModal({
					title: '提示',
					content: "前往权限设置，设置" + message,
					success: function(res) {
						if (res.confirm) {
							permision.gotoAppPermissionSetting()
						}
					}
				});
			},
			selectitem(permisionID) {
				switch (permisionID) {
					case "android.permission.CAMERA":
						if (this.CAMERA) {
							this.closeJur("摄像头")
						} else {
							this.requestAndroidPermission(permisionID)
						}
						break;
					case "android.permission.READ_EXTERNAL_STORAGE":
						if (this.READ_EXTERNAL_STORAGE) {
							this.closeJur("相册读取")
						} else {
							this.requestAndroidPermission(permisionID)
						}

						break;
					case "android.permission.WRITE_EXTERNAL_STORAGE":
						if (this.WRITE_EXTERNAL_STORAGE) {
							this.closeJur("相册写入")
						} else {
							this.requestAndroidPermission(permisionID)
						}
						break;

					case "android.permission.ACCESS_FINE_LOCATION":
						if (this.ACCESS_FINE_LOCATION) {
							this.closeJur("定位权限")
						} else {
							this.requestAndroidPermission(permisionID)
						}
						break;
				}
			},
			closeJur(permisionID) {
				uni.showModal({
					title: '提示',
					content: '确定关闭' + permisionID + "权限吗？",
					success: function(res) {
						if (res.confirm) {
							permision.gotoAppPermissionSetting()
						}
					}
				});

			},
			//查询 权限
			async judgeIosPermission(permisionID) {
				var result = await permision.requestAndroidPermission(permisionID)
				console.log(permisionID, result);
				switch (permisionID) {
					case "android.permission.CAMERA":
						this.CAMERA = result
						console.log("this.CAMERA", result)
						break;
					case "android.permission.READ_EXTERNAL_STORAGE":
						this.READ_EXTERNAL_STORAGE = result

						break;
					case "android.permission.WRITE_EXTERNAL_STORAGE":
						this.WRITE_EXTERNAL_STORAGE = result
						break;
					case "android.permission.ACCESS_FINE_LOCATION":
						this.ACCESS_FINE_LOCATION = result
						break;
				}
			},
			async requestAndroidPermission(permisionID) {
				var result = await permision.requestAndroidPermission(permisionID)
				var strStatus
				if (result == 1) {
					strStatus = "已获得授权"
					switch (permisionID) {
						case "android.permission.CAMERA":
							this.CAMERA = true
							uni.setStorageSync("CAMERA", true)
							break;
						case "android.permission.READ_EXTERNAL_STORAGE":
							this.READ_EXTERNAL_STORAGE = true
							uni.setStorageSync("READ_EXTERNAL_STORAGE", true)

							break;
						case "android.permission.WRITE_EXTERNAL_STORAGE":
							this.WRITE_EXTERNAL_STORAGE = true
							uni.setStorageSync("WRITE_EXTERNAL_STORAGE", true)
							break;

						case "android.permission.ACCESS_FINE_LOCATION":
							this.ACCESS_FINE_LOCATION = true
							uni.setStorageSync("ACCESS_FINE_LOCATION", true)
							break;
					}
				} else if (result == 0) {
					strStatus = "未获得授权"
				} else {
					strStatus = "被永久拒绝权限"
				}


			},


		}
	}
</script>

<style lang="scss" scoped>
	.pagesContent {
		position: fixed;
		top: 40rpx;
		left: 0;
		right: 0;
		bottom: 0;
		height: 98vh;
		background: linear-gradient(180deg, #E1EBFF 0%, #FAFCFF 100%);				
		overflow: hidden;
		.pagesContentView {
			position: relative;
			border-radius: 48rpx 48rpx 0 0;
			width: 100%;
			height: 100%;
			padding-top: 61.54rpx;
		}
	}

	.pageList {

		.items {
			width: 611.54rpx;
			height: 103.85rpx;
			background: #FFFFFF;
			border-radius: 23.08rpx;
			padding: 0 34.62rpx 0 30.77rpx;
			margin: 0 auto 23.08rpx;


			.left {
				float: left;
				height: 98rpx;
				line-height: 98rpx;
				font-size: 28rpx;
				color: #454655;

			}

			.right {
				float: right;
				height: 98rpx;
				line-height: 98rpx;

				view {
					float: left;
					height: 98rpx;
					line-height: 98rpx;
					font-size: 24rpx;
					color: #7C7C7C;
				}

				image {
					float: right;
					width: 32rpx;
					height: 32rpx;
					float: right;
					margin-top: 34rpx;
				}
			}
		}

	}

	.popupContent {
		height: 800rpx;
		padding-top: 36rpx;
		position: relative;
		background-color: #FFFFFF;

		.title {
			height: 44rpx;
			font-size: 32rpx;
			font-weight: 400;
			color: #000000;
			line-height: 44rpx;
			text-align: center;
			margin-bottom: 124rpx;
		}

		.close {
			width: 60rpx;
			height: 60rpx;
			position: absolute;
			top: 30rpx;
			right: 30rpx;
			z-index: 9000;
		}

		.qrCode {
			margin-bottom: 130rpx;

			image {
				border-radius: 20rpx;
				overflow: hidden;
				border: 2rpx solid rgba(0, 0, 0, 0.1);
				width: 294rpx;
				height: 294rpx;
				margin: 0 auto 34rpx;
				display: block;
			}

			view {
				height: 40rpx;
				font-size: 28rpx;
				font-family: PingFangSC-Regular, PingFang SC;
				font-weight: 400;
				color: rgba(0, 0, 0, 0.3);
				line-height: 40rpx;
				text-align: center;

			}
		}

		.btn {
			width: 670rpx;
			height: 88rpx;
			line-height: 88rpx;
			text-align: center;
			background: #414654;
			margin: 0 auto 40rpx;
			text-align: center;
			font-size: 28rpx;
			font-family: PingFangSC-Regular, PingFang SC;
			font-weight: 400;
			color: #FFFFFF;
		}

	}

	.adBox {
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
	}
</style>